//
//  ZBResultViewModel.m
//  BaseRACDemo
//
//  Created by Angel_Yan on 2018/3/28.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBResultViewModel.h"
#import "ZBResultCellViewModel.h"
#import "ZBHTTPService+ResultData.h"

@interface ZBResultViewModel ()

@property (nonatomic, readwrite, copy) NSArray *noteLists;

@end

@implementation ZBResultViewModel

- (void)initialize {
    [super initialize];
    self.title = @"发送记录";
    
    self.shouldMultiSections = YES;
    self.style = UITableViewStylePlain;
    //允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    //允许上啦加载
    self.shouldPullUpToLoadMore = YES;
    ///没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = NO;
    
    @weakify(self);

    [self.requestRemoteDataCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        
        
    }];
    
    RAC(self, noteLists) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    //< 转模数据源
    RAC(self,dataSource) = [RACObserve(self, noteLists) map:^(NSArray * noteLists) {
        @strongify(self)
        return [self dataSourceWithResultData:noteLists];
    }];
    
    /// 选中cell 跳转的命令
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        @strongify(self);
        NSLog(@"点击的是 %ld",(long)indexPath.row);
        return [RACSignal empty];
    }];
}


/// 请求数据
- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    NSArray * (^mapResultData)(ZBResultModel *) = ^(ZBResultModel *resultModel) {
        NSMutableArray *aryData = @[].mutableCopy;
        if (page == 1) {
            /// 下拉刷新
            [aryData addObjectsFromArray:resultModel.list];
        } else {
            /// 上拉加载
            [aryData addObjectsFromArray:@[(self.noteLists ?: @[]).rac_sequence, resultModel.list.rac_sequence].rac_sequence.flatten.array];
        }
        return aryData;
    };
    
    /// 请求网络数据
    ZBUserOwn *userOwn = [self.services.client currentUser];
    return [[self.services.client fetchResultWithUserToken:userOwn.token size:self.perPage page:page] map:mapResultData];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithResultData:(NSArray *)liveRooms {
    if (MHObjectIsNil(liveRooms) || liveRooms.count == 0) return nil;
    NSArray *viewModels = [liveRooms.rac_sequence map:^(ZBResultItemModel *itemModel) {
        ZBResultCellViewModel *viewModel = [[ZBResultCellViewModel alloc] initWithItemModel:itemModel];
        return viewModel;
    }].array;
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    for (ZBResultCellViewModel *viewModel in viewModels) {
        if (mArr.count == 0) {
            ///< 第一个
            NSMutableArray *mArrFirst = [[NSMutableArray alloc] init];
            [mArrFirst addObject:viewModel];
            [mArr addObject:mArrFirst];
        } else {
            ///< 寻找
            NSInteger index = -1;
            for (int i = 0; i < mArr.count; i ++) {
                NSMutableArray *temp = mArr[i];
                ZBResultCellViewModel *tempModel = [temp firstObject];
                if ([tempModel.itemModel.scheduleId isEqualToString:viewModel.itemModel.scheduleId]) {
                    index = i;
                    break;
                }
            }
            
            if (index == -1) {
                /// 增加
                NSMutableArray *mArrTemp = [[NSMutableArray alloc] init];
                [mArrTemp addObject:viewModel];
                [mArr addObject:mArrTemp];
            } else {
                ///< 插入
                NSMutableArray *mArrTemp = [mArr objectAtIndex:index];
                [mArrTemp addObject:viewModel];
            }
        }
    }
    return mArr ?: @[] ;
}

@end
